package gsw.demo.greendao;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.greendao.query.Query;

import java.sql.Date;

import gsw.tool.sqlite.R;


public class ActivitySqlite2 extends AppCompatActivity {
    //显示查询结果
    TextView result;
    private NoteDao noteDao;
    private Query<Note> notesQuery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);
        result = findViewById(R.id.showResult);

        // get the note DAO
        DaoSession daoSession = DaoSessionFactory.getDaoSession(this);
        noteDao = daoSession.getNoteDao();
    }

    /**
     * 显示查询结果
     */
    private void showResult() {
        // query all notes, sorted a-z by their text
        notesQuery = noteDao.queryBuilder().orderAsc(NoteDao.Properties.Text).build();
        result.setText(notesQuery.list().toString());
    }

    /**
     * 添加数据
     *
     * @param view
     */
    public void clickInsert(View view) {
        Note note = new Note();
        note.setText("tom");
        note.setDate(new Date(System.currentTimeMillis()));
        noteDao.insert(note);
        showResult();
    }

    /**
     * 删除数据
     *
     * @param view
     */
    public void clickDelete(View view) {
        noteDao.deleteAll();
        showResult();
    }

    /**
     * 修改数据
     *
     * @param view
     */
    public void clickUpdate(View view) {
        Note note = new Note();
        note.setId((long) 1);
        note.setText("lily");
        note.setDate(new Date(108, 04, 20));
        noteDao.update(note);
        showResult();
    }

    /**
     * 查询数据
     *
     * @param view
     */
    public void clickSelect(View view) {
        showResult();
    }

    /**
     * 多用户登录
     *
     * @param view
     */
    public void clickLogin(View view) {
    }

    /**
     * 分库插入
     *
     * @param view
     */
    public void clickSubInsert(View view) {
    }

    /**
     * 数据库升级
     *
     * @param view
     */
    public void clickUpdateDB(View view) {
    }
}








